package com.juzhencms.apps.busi.web;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.juzhencms.apps.base.busi.service.WeixinApiService;
import com.juzhencms.apps.base.common.database.DBTable;
import com.juzhencms.apps.base.common.util.WebUtil;


@Controller
@RequestMapping("/apiToken")
public class ApiTokenController  extends BaseController{
	@Autowired
	private WeixinApiService weixinApiService;
	
	@RequestMapping(value="/gridlistData",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String gridlistData(HttpServletRequest request){
		Map data=new HashMap();
		List whereParamList=new ArrayList();
		String where="where 1 ";
		//搜索参数
		
		if(request.getParameter("appid")!=null && !request.getParameter("appid").toString().trim().equals("")){
			where+=" and appid = ? ";
			whereParamList.add(request.getParameter("appid"));
		}
		if(request.getParameter("token_type")!=null && !request.getParameter("token_type").toString().trim().equals("")){
			where+=" and token_type = ? ";
			whereParamList.add(request.getParameter("token_type"));
		}
		
		
		//分页处理
		int pageIndex=1;
		if(request.getParameter("pageIndex")!=null && !request.getParameter("pageIndex").toString().trim().equals("")){
			pageIndex=new Integer(request.getParameter("pageIndex")).intValue();
		}
		int pageSize=10;
		if(request.getParameter("pageSize")!=null && !request.getParameter("pageSize").toString().trim().equals("")){
			pageSize=new Integer(request.getParameter("pageSize")).intValue();
		}
		int start=0;
		if(pageIndex>1){
			start=(pageIndex-1)*pageSize;
		}
		whereParamList.add(start);
		whereParamList.add(pageSize);
		
		//排序参数 
		String sortField="id";
		String sortOrder="desc";
		if(request.getParameter("sortField")!=null && !request.getParameter("sortField").equals(""))
			sortField = request.getParameter("sortField");
		if(request.getParameter("sortOrder")!=null && !request.getParameter("sortOrder").equals(""))
			sortOrder = request.getParameter("sortOrder");
		
		//查询列表数据
		List dataList=this.getCommonService().getJdbcDao().querySql("select * from "+DBTable.apiToken+" "+where+" order by "+sortField+" "+sortOrder+" limit ?,?", whereParamList.toArray());
		if(dataList!=null && dataList.size()>0){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			for(int i=0;i<dataList.size();i++){
				Map row=(Map)dataList.get(i);
				if(row.get("create_time")!=null) {
					Date date = (Date)row.get("create_time");
					row.put("create_time",sdf.format(date));
				}
			}
		}
		
		//查询总条数
		Map countRow=this.getCommonService().getJdbcDao().queryRow("select count(id) as count_num from "+DBTable.apiToken+" "+where, whereParamList.subList(0, whereParamList.size()-2).toArray());
		int recordsTotal=0;
		if(countRow.get("count_num")!=null){
			recordsTotal=new Integer(countRow.get("count_num").toString()).intValue();
		}
		//拼装返回的数据
		data.put("data", dataList.toArray());
		data.put("itemsCount", recordsTotal);
		return WebUtil.resultString(data,request);
	}
	
	@RequestMapping(value="/refresh",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String refresh(HttpServletRequest request){
		Map result=new HashMap();
		String appid=request.getParameter("appid");
		if(appid!=null && !appid.equals("")) {
			Map site=this.getCommonService().getJdbcDao().queryRow("select * from "+DBTable.site+" where del_flag=0 and weixin_appid=? order by id asc limit 0,1", new Object[] {appid});
			if(site!=null && site.get("weixin_appsecret")!=null && !site.get("weixin_appsecret").equals("")) {
				String accessToken=weixinApiService.refreshAccessToken(site.get("weixin_appid").toString(), site.get("weixin_appsecret").toString());
				if(accessToken!=null && !accessToken.equals("")) {
					result.put("result", 1);
					result.put("msg", "保存成功");
				}else {
					result.put("result", 0);
					result.put("msg", "更新失败");
				}
			}else {
				result.put("result", 0);
				result.put("msg", "微信公众号配置有误");
			}
		}else {
			result.put("result", 0);
			result.put("msg", "参数有误");
		}
		return WebUtil.resultString(result,request);
	}
	
}
